Typical Usage:
Required bean name is "PWM1".
There are the following four typical usage modes:
(1)
The easiest usage of the bean is shown below on a simple output signal generation with predefined period and duty.
It's possible to change duty using method SetRatio.
MAIN.C
void main(void)
{
unsigned int i, j;
for( i = 0; i<65535; ++i ) {
PWM1_SetRatio16( i ); //change duty
for( j=0; j<65535; ++j ); //delay
}
for(;;);
}
Note: The bean may be also used without any method and event - only for settings of the output signal period and duty.
(2)
Usually it's better to change the duty after the end of period, because this helps to avoid noises in the output signal.
The property "Interrupt service/event" must be enabled for the support of OnEnd event.
EVENTS.C
unsigned ratio = 0;
void PWM1_OnEnd(void)
{
PWM1_SetRatio16( ++ratio ); //change duty
}
(3)
There are other advanced methods for changing duty at runtime.
It is possible to select one duty from the predefined list using SetRatioMode
method or set the value of the interval using methods SetDutyTicks and SetDutyTime (please refer to Bean Timing Dialog).
In the following example the duty is switched each 255-th period.
The mode is set sequentially from three predefined values.
EVENTS.C
int rpt_cntr = 255, mode = 0;
void PWM1_OnEnd(void)
{
if( --rpt_cntr==0 ) { // If the event was generated 255times
if( ++mode==3 ) mode = 0; // Select next mode of the period
PWM1_SetRatioMode( mode );// Set new period mode
rpt_cntr = 255; // Set repeat counter
}
}
(4)
Bean initial level of the output signal may be changed using SetValue and ClrValue methods.
These methods may be used only if the bean is disabled.
MAIN.C
void main(void)
{
/* output signal synchronization */
PWM1_Disable(); /* disable the bean */
PWM1_ClrValue(); /* set output level to LOW */
PWM1_Enable(); /* enable the bean */
}
Version specific information for HCS12, HCS12X derivatives
Emergency shutdown case
When the Emergency shutdown and Emergency interrupt service/event are enabled
the bean generates OnShutdown event.
Bean provides two methods for the PWM status detection and restarting signal generation.
EVENTS.C
void PWM1_OnShutdown(void)
{
/* Test current shutdown status */
if (PWM1_GetShutdownStatus()) {
/* Emergency shutdown is asserted,
PWM signal generation is stopped! */
...
} else {
/* Emergency shutdown is de-asserted,
signal generation can be restarted */
PWM1_RestartPWM();
}
}
For more about typical usage of the bean code please refer to the page Bean Code Typical Usage.
|